package com.xiaofei.producerconsumer;

import java.util.Random;
import java.util.concurrent.BlockingQueue;

public class Consumer implements Runnable{
	private BlockingQueue<Data>blockingQueue;
	private static final int SLEEP_TIME = 5000;
	public Consumer(BlockingQueue<Data>blockingQueue) {
		this.blockingQueue = blockingQueue;
	}
	@Override
	public void run() {
		try {
			Random r = new Random();
			while(true){
				Data data = blockingQueue.take();
				if(data!=null){
					System.out.println(data+" consumed by "+Thread.currentThread().getName());
				}
				Thread.sleep(r.nextInt(SLEEP_TIME));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
